//
//  main.m
//  05-循环遍历
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    //定义一个数组, 包含5个字符串对象, 倒序取出数组中所有元素, 并存储到另一可变数组中。
    // 1> 创建数组
    // 2> 创建可变数组
    NSArray *arr = @[@"我", @"你", @"3", @"4", @"5"];
    NSMutableArray *array = [NSMutableArray array];
    for (NSUInteger i = 0; i < arr.count; i++) {
        [array addObject:arr[arr.count - 1 - i]];
    }
     // NSLog(@"%@",array);
    
    //定义一个字典,  于存储 个 的name,sex, age三个数 据, 完成对其三个数据的输出。
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"糖", @"name", @"m", @"sex", @"18", @"age", nil];
    for (NSInteger i = 0;i < dictionary.count; i++) {
        NSString *key = dictionary.allKeys[i];
        NSLog(@"key = %@ value = %@",key, [dictionary objectForKey:dictionary.allKeys[i]]);
        }
    
    // 枚举器(遍历方式)
    // NSEnumerator依赖于集合类型(数组, 字典, 集合), 不能单独使用
    NSEnumerator *stringArrayEnuerator = [arr objectEnumerator];
    id value = nil;
    while ((value = [stringArrayEnuerator nextObject]) != nil) {
       // NSLog(@"%@",value);
    }
    // 倒序枚举
    NSEnumerator *reverse1 = [arr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [reverse1 nextObject]) {
       // NSLog(@"%@",value1);
    }
    // 字典枚举器
    NSEnumerator *dicEnumerator = [dictionary objectEnumerator];
    id dicValue = nil;
    while (dicValue = [dicEnumerator nextObject]) {
       // NSLog(@"%@",dicValue);
    }
    // 集合枚举器
    NSSet *set = [NSSet setWithObjects:@"110", @"120", @"119", @"12345",  nil];
    NSEnumerator *setEnuerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnuerator nextObject]) {
      //  NSLog(@"setValue = %@", setValue);
    }
    
    // 快速枚举
    for (NSString *string in arr) {
        NSLog(@"%@", string);
    }
    // 字符串
    NSMutableString *stri = [NSMutableString string];
    for (NSString *string in arr) {
        [stri appendString:string];
    }
    NSLog(@"拼接%@",stri);
    
    // 快速枚举一个集合
    for (NSString *anyObject in set) {
        NSLog(@"anyObject = %@", anyObject);
    }
    
    //排序
    // 参数1 Key:排序条件
    // 参数2 acending: YES(升序) or NO(降序)
    NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
    // 数组有排序的方法(sortedArrayUsingDescriptors)
    // 参数为一个排序条件的数组
  array1 = [array1 sortedArrayUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", array1);
   
    // 排序1: 可变数组和不可变数组通过sort排序调用的方法不同
    // 1.不可变数组, 排序后需要生成新的数组
    // 2.可变数组, 排序直接可以在本身之上修改
    Person *per = [[Person alloc] init];
    per.name = @"谢文君";
    per.age = 100;
    Person *per1 = [[Person alloc] init];
    per1.name = @"张喆";
    per1.age = 200;
    Person *per2 = [[Person alloc] init];
    per2.name = @"熊浩林";
    per2.age = 300;
    NSArray *perArray = [NSArray arrayWithObjects:per, per1, per2, nil];
    // 根据name排序
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
 perArray = [perArray sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"%@",perArray);
    // 根据age排序
    NSSortDescriptor *perSort1 = [[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];
    perArray = [perArray sortedArrayUsingDescriptors:@[perSort1]];
    NSLog(@"%@",perArray);
    
    //排序2
    NSArray *sortString = @[@"1", @"2", @"3", @"4"];
   sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@",sortString);

    return 0;
}
